#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifdef RVS_TEST

#include "rvs_socket.h"
#define MYPORT1	2001
#define MYPORT2	2002
#define	REPORT1	3001
#define	REPORT2	3002
#define	socket	rvs_socket
#define	close	rvs_close
#define	bind	rvs_bind
#define	sendto	rvs_sendto
#define	recvfrom	rvs_recvfrom
#define	select	rvs_select
#define listen  rvs_listen
#define accept  rvs_accept
static inline void rvs_perror(const char* text)
{
    fprintf( stderr, "%s : Error = %d\n", text, rvs_errno);
}
#define	perror	rvs_perror

#else

#define MYPORT1	3001
#define MYPORT2	3002
#define	REPORT1	2001
#define	REPORT2	2002

#endif

static int myport1=MYPORT1, myport2=MYPORT2;

void usage(char* cmd)
{
#define USAGE   "\
[USAGE]\n\
    %s [-1 <myport1>][-2 <myport2>][-h]\n"

    fprintf(stderr, USAGE, cmd);
}

int main(int argc, char** argv)
{
    int sock1=-1, sock2=-1, len, cont;
    struct sockaddr_in sin1, sin2, remote;
    char cmd[16];
    char buffer[2048];
    char *prog = argv[0];

    for (argc--, argv++; argc>0; argc--, argv++) {
        if (**argv != '-') {
            usage(prog);
            exit(1);
        }
        switch (*(*argv+1)) {
        case '1':
            argc--; argv++;
            myport1 = atoi(*argv);
            break;
        case '2':
            argc--; argv++;
            myport2 = atoi(*argv);
            break;
        default:
            usage(prog);
            exit(1);
        }
    }

    fprintf (stdout, "===== %s Started: MyPort#1=%d, MyPort#2=%d =====\n",
            prog, myport1, myport2);

    sock1 = socket (AF_INET, SOCK_DGRAM, 0);
    if (sock1 < 0) {
        perror("socket");
        goto sockerr;
    }
    sock2 = socket (AF_INET, SOCK_DGRAM, 0);
    if (sock2 < 0) {
        perror("socket");
        goto sockerr;
    }

    memset (&sin1, 0, sizeof(sin1));
    memset (&sin2, 0, sizeof(sin2));
    memset (&remote, 0, sizeof(remote));
    remote.sin_family = sin2.sin_family = sin1.sin_family = AF_INET;
    sin2.sin_addr.s_addr = sin1.sin_addr.s_addr = INADDR_ANY;
    remote.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin1.sin_port = htons(myport1);
    sin2.sin_port = htons(myport2);
    remote.sin_port = htons(REPORT1);

    if (bind (sock1, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) {
        perror("bind");
        goto sockerr;
    }
    if (bind (sock2, (struct sockaddr*)&sin2, sizeof(sin2)) < 0) {
        perror("bind");
        goto sockerr;
    }

    fprintf (stderr, "[s/r] > ");

    cont=1;
    while (cont && fgets(cmd, 16, stdin)) {
        int sock;
        struct sockaddr *addr;
        struct sockaddr_in from;
        socklen_t addrlen = sizeof(struct sockaddr_in);
        socklen_t fromlen = sizeof(from);
        int nev;
        fd_set readfds;
        int nfds=0;
        struct timeval tv, *tvp;
    
        switch(cmd[0]) {
        case 's':
        case 'S':
            fprintf (stderr, "from (1)%d / (2)%d ? ", myport1, myport2);
            fgets (cmd, 16, stdin);
            if (cmd[0] == '1') {
                 sock = sock1;
            } else {
                 sock = sock2;
            }
            fprintf (stderr, "to port# > ");
            fgets (cmd, 16, stdin);
            remote.sin_port = htons(atoi(cmd));
            addr = (struct sockaddr*)&remote;
            fprintf (stderr, "sending data >> ");
            if (fgets (buffer, 2048, stdin)) {
                len = sendto (sock, buffer, strlen(buffer)-1, 0, addr, addrlen);
                if (len > 0) {
                    fprintf (stderr, "sendto %s:%d\n",
                        inet_ntoa(((struct sockaddr_in*)addr)->sin_addr),
                        ntohs(((struct sockaddr_in*)addr)->sin_port));
                    fprintf( stderr, " - %.*s\n", len, buffer);
                } else {
                    perror("sendto");
                }
            } else {
                cont = 0;
            }
            break;
        case 'r':
        case 'R':
            FD_ZERO(&readfds);
            FD_SET(sock1, &readfds);
            FD_SET(sock2, &readfds);
            nfds = (sock1>sock2)?(sock1+1):(sock2+2);
            
            fprintf(stdout, "Wait Time(sec) > ");
            fgets (cmd, 16, stdin);
            if (cmd[0] == '\n') {
                tvp = NULL;
            } else {
                tv.tv_sec = atoi(cmd);
                tv.tv_usec = 0;
                tvp = &tv;
            }
            nev = select (nfds, &readfds, NULL, NULL, tvp);
            if (nev < 0) {
                perror("select");
            } else if (nev == 0) {
                fprintf( stderr, "select: Timeout\n");
            } else {
                int i;
                sock = sock1;
                for ( i=0; i<2; i++, sock=sock2) {
                    if (FD_ISSET(sock, &readfds)) {
                        if ((len = recvfrom(sock, buffer, 2048, 0,
                                  (struct sockaddr*)&from, &fromlen)) < 0) {
                            perror("recvfrom");
                            break;
                        }
                        if (len == 0) cont=0;
                        fprintf( stderr, "recvfrom(sock%d) %s:%d\n", i+1,
                               inet_ntoa(from.sin_addr), ntohs(from.sin_port));
                        fprintf( stderr, " - %.*s\n", len, buffer);
                    }
                }
            }
            break;
        case 'q':
        case 'Q':
            cont=0;
            break;
        }
        fprintf (stderr, "[s/r] > ");
    }

    close(sock1);
    close(sock2);

    return 0;

sockerr:
    if (sock1 >= 0) close(sock1);
    if (sock2 >= 0) close(sock2);
    exit(1);
}
